package com.icode.web.validator;

import com.icode.core.dto.user.UserDTO;
import com.icode.core.model.user.Customer;
import com.icode.core.model.user.User;
import com.icode.core.shared.ValidateNumHandle;
import com.icode.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sunk
 * Date: 14-12-27
 * Time: 下午6:38
 * To change this template use File | Settings | File Templates.
 */
@Component
public class RegisterFormValidator implements Validator {
    @Autowired
    private UserService userService;
    @Autowired
    private HttpSession session;

    @Override
    public boolean supports(Class<?> clazz) {
        return UserDTO.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        UserDTO userDTO = (UserDTO) target;
        String phoneNum = userDTO.getUsername();
        String password = userDTO.getPassword();
        String rePassword = userDTO.getRePassword();
        String validateNum = userDTO.getValidationNum();

        String userVerify = userDTO.getVerify();
        Map<String, Object> getValidateNum = ValidateNumHandle.getValidateNum();
        boolean isAgree = userDTO.isAgree();
        if (isNull(phoneNum) || isNull(password) || isNull(rePassword) || isNull(validateNum)) {
            return;
        }
        if (isExistPhoneNum(phoneNum)) {
            errors.rejectValue("username", null, "号码已注册!");
        }
        if (!isPasswordSame(password, rePassword)) {
            errors.rejectValue("rePassword", null, "密码不一致!");
        }
        if (!isAgree) {
            errors.rejectValue("agree", null, "请阅读条款!");
        }
        if (getValidateNum.isEmpty()) {
            errors.rejectValue("validationNum", null, "验证码错误");
            return;
        }
        if (!isPasswordSame(validateNum, getValidateNum.get(phoneNum).toString().substring(0, 4))) {
            errors.rejectValue("validationNum", null, "验证码错误");
        }
        if (!isEffective(getValidateNum.get(phoneNum).toString().substring(4))) {
            errors.rejectValue("validationNum", null, "验证码已过期");
        }
        String verify = session.getAttribute("verify").toString();
        if (!verify.equalsIgnoreCase(userVerify)) {
            errors.rejectValue("verify", null, "验证码错误");
        }
    }

    private boolean isPasswordSame(String password, String rePassword) {
        if (password.equals(rePassword)) {
            return true;
        }
        return false;
    }

    private static boolean isNull(String s) {
        if ("".equals(s)) {
            return true;
        }
        return false;
    }

    private boolean isExistPhoneNum(String phoneNum) {
        Customer customer = userService.findByPhoneNum(phoneNum);
        return customer != null ? true : false;
    }

    private boolean isEffective(String nowTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long day = 0;
        long hour = 0;
        long min = 0;
        long s = 0;
        try {
            Date date = sdf.parse(nowTime);
            String strNow = sdf.format(new Date());
            Date now = sdf.parse(strNow);
            long l = now.getTime() - date.getTime();
            day = l / (24 * 60 * 60 * 1000);
            hour = (l / (60 * 60 * 1000) - day * 24);
            min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
            s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (day < 1 && hour < 1 && min < 4 && s <= 60) {
            return true;
        }
        return false;
    }
}
